﻿using MVC5Test.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC5Test.Controllers
{
    /// <summary>
    /// 6个内置对象
    /// 1.Request
    /// 2.Response
    /// 3.Session
    /// 4.Cookie
    /// 5.Application
    /// 6.Server
    /// </summary>
    public class UserController : Controller
    {
        // GET: User
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CreateUser(CurrentUser user)
        {
            return View();
        }

        /// <summary>
        /// 通过Request.QueryString
        /// </summary>
        /// <returns></returns>
        public ActionResult GetQueryString()
        {
            ContentResult content = Content(Request.QueryString["name"]+"-"+Request.QueryString["age"]);
            return content;
        }

        public ActionResult PostForm()
        {
            ContentResult content = Content(Request.Form["LoginName"]);
            return content;
        }

        public ActionResult UploadFile(HttpPostedFileBase[] files)
        {
            try
            {
                //Request.Files["picture"].SaveAs(Request.MapPath($"~/Upload/Picture/{Request.Files["picture"].FileName}"));
                foreach (HttpPostedFileBase item in files)
                {
                    item.SaveAs(Request.MapPath($"~/Upload/Picture/{item.FileName}"));
                }
            }
            catch (Exception)
            {

                Content("上传失败！！！");
                throw;
            }
            return Content("上传成功");
        }

        public ActionResult UploadFiles()
        {
            try
            {
                var files = Request.Files.GetMultiple("files");
                //Request.Files["picture"].SaveAs(Request.MapPath($"~/Upload/Picture/{Request.Files["picture"].FileName}"));
                foreach (HttpPostedFileBase item in files)
                {
                    item.SaveAs(Request.MapPath($"~/Upload/Picture/{item.FileName}"));
                }
            }
            catch (Exception)
            {

                Content("上传失败！！！");
                throw;
            }
            return Content("上传成功");
        }


        /// <summary>
        /// Response 对象
        /// </summary>
        public void GetResponse()
        {
            Response.Write("我第一次遇到你");
        }

        /// <summary>
        /// Seesion对象
        /// </summary>
        /// <returns></returns>
        public ActionResult SetSession()
        {
            var user = Request.Form["user"];
            Session["user"] = user;
            return Content($"会话中的session数据设置成功");
        }

        public ActionResult GetCurrentSession()
        {
            var context = string.Empty;
            try
            {
                context = $"会话中的数据是：{Session["user"].ToString()}";

            }
            catch (Exception)
            {

                context = "当前登录状态已经退出！！！";
            }
            return Content(context);

        }

        public ActionResult AbortSession()
        {
            Session.Abandon();
            return Content($"当前绘画结束退出登录状态");
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <returns></returns>
        public ActionResult SetCookie()
        {
            Response.Cookies.Add(new HttpCookie("token", "aaaaddddcccc") { Expires = DateTime.Now.AddDays(1)});
            return Content("设置Cookie完成");
        }


        #region Application
        public void SaveApplication()
        {
            HttpContext.Application["user"] = "321";
            
        }

        public ActionResult GetApplication()
        {
            var context =  HttpContext.Application["user"].ToString();
            return Content(context);
        }

        #endregion



    }
}